import { computed, ref, unref, watch } from 'vue'
import request from '/@/utils/request';
/**
 * 获取详情
 * @param url 请求地址 
 * @param immediate  是否在id变动时加载，默认: `true` 
 */
function useDetail({ url, immediate = true}: any) {
    const id = ref(undefined), detail = ref({})

    //如果immediate为true，那id改变就加载数据
    immediate && watch(id, loadDetail)

    async function loadDetail() {
        // TODO: 请求api及赋值
        await request({url: unref(url), params: { id: unref(id) }}).then(res => {
            if(res) {
                detail.value = res.data
            }
        })
    }

    return {
        id,
        detail,
        loadDetail,
    }
}